package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.lovo.bean.Service;
import com.lovo.bean.Staff;
import com.lovo.bean.User;
import com.lovo.service.IServiceService;
import com.lovo.service.serviceImpl.ServiceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.geom.Area;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ServiceServlet", urlPatterns = "/service.do")
public class ServiceServlet extends HttpServlet {
    IServiceService ssi = new ServiceServiceImpl ();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet (request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding ("UTF-8");
        String method = request.getParameter("method");
        if(method == null || method.equalsIgnoreCase("")){
        }
        else if (method.equalsIgnoreCase ("service_page")){
            servicePage(request,response);
        }
        else if (method.equalsIgnoreCase ("service_add")){
            serviceAdd(request,response);
        }
        else if (method.equalsIgnoreCase ("service_update")){
            serviceUpdate(request,response);
        }
        else if (method.equalsIgnoreCase ("service_query")){
            serviceQuery(request,response);
        }
        else if (method.equalsIgnoreCase ("staff_show")){
            staffShow(request,response);
        }
        else if (method.equalsIgnoreCase ("service_del")){
            serviceDel(request,response);
        }

    }

    //    删除服务
    private void serviceDel(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException   {
        int serviceId = Integer.parseInt (request.getParameter ("serviceId"));
        boolean flag = ssi.delService(serviceId);
        response.getWriter().write(String.valueOf(flag));

    }

    //    查询所有服务员
    private void staffShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        List<Staff> showStaff = ssi.getStaff();
        System.out.println ("showStaff = " + showStaff);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(showStaff);
        response.getWriter().write(json);
    }

    //    按条件查询并分页显示
    private void serviceQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = 1;
        int pageSize = 10;
        String page = request.getParameter("page");
        if(page != null){
            pageNo = Integer.parseInt(page);
        }
        String limit = request.getParameter("limit");
        if(limit != null){
            pageSize = Integer.parseInt(limit);
        }
        Service service = new Service ();
        service.setServiceName (request.getParameter ("serviceName"));
        System.out.println (request.getParameter ("serviceName"));

        PageInfo<Service> query = ssi.getPageByQuery(pageNo,pageSize,service);
        System.out.println ("query = " + query);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(query);
        response.getWriter().write(json);

    }

    //    修改
    private void serviceUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Service service = new Service ();
        service.setServiceId (Integer.parseInt (request.getParameter ("serviceId")));
        service.setServiceName (request.getParameter ("serviceName"));
        service.setServiceTime (request.getParameter ("serviceTime"));
        service.setServiceWeight (request.getParameter ("serviceWeight"));
        service.setServiceFix (request.getParameter ("serviceFix"));
        service.setServiceUsetime (request.getParameter ("serviceUsetime"));
        service.setFkStaffId (Integer.parseInt (request.getParameter ("fkStaffId")));
        service.setServicePrice (request.getParameter ("servicePrice"));

        System.out.println ("service = " + service);


        boolean flag = ssi.updateService(service);
        response.getWriter().write(String.valueOf(flag));

    }

    //    添加
    private void serviceAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Service service = new Service ();
        service.setServiceName (request.getParameter ("serviceName"));
        service.setServiceTime (request.getParameter ("serviceTime"));
        service.setServiceWeight (request.getParameter ("serviceWeight"));
        service.setServiceFix (request.getParameter ("serviceFix"));
        service.setServiceUsetime (request.getParameter ("serviceUsetime"));
        service.setFkStaffId (Integer.parseInt (request.getParameter ("fkStaffId")));
        service.setServicePrice (request.getParameter ("servicePrice"));

        boolean flag = ssi.addService(service);
        response.getWriter().write(String.valueOf(flag));
    }

    //    分页显示所有
    private void servicePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = 1;
        int pageSize = 10;
        String page = request.getParameter("page");
        if(page != null){
            pageNo = Integer.parseInt(page);
        }
        String limit = request.getParameter("limit");
        if(limit != null){
            pageSize = Integer.parseInt(limit);
        }

        PageInfo<Service> all = ssi.getPage(pageNo,pageSize);
        System.out.println ("all = " + all);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(all);
        response.getWriter().write(json);
    }
}
